package com.hyt.it.ogt.kq.main.controller.gov;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.exception.GovInvalidRequestException;
import com.hyt.it.ogt.kq.service.gov.model.entity.OfficePlace;
import com.hyt.it.ogt.kq.service.gov.model.entity.OfficeRoom;
import com.hyt.it.ogt.kq.service.gov.model.param.OfficePlaceParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.RoomPageVO;
import com.hyt.it.ogt.kq.service.gov.service.IOfficePlaceService;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeRoomService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskPlaceService;
import com.hyt.model.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考点信息 前端控制器
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
//@RestController
@Api(tags = "120.考点接口", value = "考点接口")
@ApiSort(value = 120)
//@RequestMapping("/kw/gov/place")
@Slf4j
public class PlaceController extends BaseController {

    @Autowired
    private IOfficePlaceService iOfficePlaceService;

    @Autowired
    private IOfficeRoomService iOfficeRoomService;
    @Resource
    private ITaskPlaceService iTaskPlaceService;

    @ApiOperation(
            value = "20.1 添加考点",
            notes = "30000:添加考点成功; 33201:增加考点失败; 33202:添加考点参数校验错误; 30153:当前机构下存在名称相同的考点"
    )
    @ApiOperationSort(value = 1)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(@Valid @RequestBody OfficePlaceParam officePlaceParam) {
        log.info("# 添加考点参数 ： {}", officePlaceParam.toString());
        try {
            String officeId = getOfficeId();
            iOfficePlaceService.add(officePlaceParam, officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 添加考点异常: ", kwGovException);
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (GovInvalidRequestException invalidRequestException) {
            log.error("# 请求异常: ", invalidRequestException);
            return ApiResponse.builder()
                    .code(invalidRequestException.getCode())
                    .build();
        } catch (Exception e) {
            log.error("# 添加考点异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.PLACE_ADD_FAIL.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "20.2 查询考点详细信息",
            notes = "30000:查询考点详细信息成功; 30136:查询考点详细信息异常; 30137:考点id不能为空"
    )
    @ApiOperationSort(value = 2)
    @RequestMapping(value = "/query/{placeId}", method = RequestMethod.GET)
    public ApiResponse<Object> query(@PathVariable String placeId) {
        log.info("# 查询考点详细信息参数 ： {}", placeId);
        try {
            //参数校验
            if (StringUtils.isEmpty(placeId)) {
                //考点id不能为空
                return ApiResponse.builder()
                        .code(ResponseCode.PLACE_QUERY_ID_IS_NULL.getCode())
                        .build();
            }
            QueryWrapper<OfficePlace> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(OfficePlace::getId, placeId)
                    .eq(OfficePlace::getDelFlag, false);
            OfficePlace officePlace = iOfficePlaceService.getOne(queryWrapper);
            if (ObjectUtils.isEmpty(officePlace)) {
                //考点已不存在
                return ApiResponse.builder()
                        .code(ResponseCode.PLACE_QUERY_NON_EXISTENT.getCode())
                        .build();
            }
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(officePlace)
                    .build();
        } catch (Exception e) {
            log.error("# 查询考点详细信息异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.PLACE_QUERY_INFO_ERROR.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "20.3 查询机构下所有考点",
            notes = "30000:查询机构下所有考点成功; 30139:查询机构下所有考点异常"
    )
    @ApiOperationSort(value = 3)
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public ApiResponse<Object> getAll(String areaId) {
        try {
            String officeId = getOfficeId();
            List<Map<String, String>> idNameList = iOfficePlaceService.getAll(officeId, areaId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(idNameList)
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 查询考点详细信息异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (GovInvalidRequestException e) {
            log.error("# 请求异常:", e);
            return ApiResponse.builder()
                    .code(e.getCode())
                    .build();
        } catch (Exception e) {
            log.error("# 查询考点详细信息异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ORGANIZATION_QUERY_ALL_PLACE_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "20.4 查询考点信息分页数据",
            notes = "30000:查询考点信息分页数据成功; 30136:查询考点信息分页数据异常"
    )
    @ApiOperationSort(value = 4)
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ApiResponse<Object> page(PageParam<RoomPageVO> pageParam) {
        log.info("# 查询考点信息分页数据参数 ： {}", pageParam.toString());
        try {
            String officeId = getOfficeId();
            Page<RoomPageVO> page = iOfficePlaceService.page(pageParam, officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(page)
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 查询考点信息分页数据异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 查询考点信息分页数据异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.PLACE_PAGE_QUERY_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "20.5 检查考点下是否已经存在考场",
            notes = "30000:检查考点下是否已经存在考场成功; 30156:检查考点下是否已经存在考场异常; 30157:考点id不能为空"
    )
    @ApiOperationSort(value = 5)
    @RequestMapping(value = "/checkRoom", method = RequestMethod.GET)
    public ApiResponse<Object> checkRoom(String placeId) {
        log.info("# 检查考点下是否已经存在考场参数 ： {}", placeId);
        try {
            if (StringUtils.isEmpty(placeId)) {
                //考点id不能为空
                return ApiResponse.builder()
                        .code(ResponseCode.PLACE_QUERY_ID_IS_NULL.getCode())
                        .build();
            }
            QueryWrapper<OfficeRoom> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(OfficeRoom::getPlaceId, placeId)
                    .eq(OfficeRoom::getDelFlag, false);
            int count = iOfficeRoomService.count(queryWrapper);
            boolean haveRoom = false;
            if (count > 0) {
                haveRoom = true;
            }
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(haveRoom)
                    .build();
        } catch (Exception e) {
            log.error("# 检查考点下是否已经存在考场异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.PLACE_CHECK_ROOM_EXISTENT.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "20.6 修改考点",
            notes = "30000:修改考点成功; 30158:修改考点异常; 30159:修改考点参数校验错误; 30160:当前机构下存在名称相同的考点; 30161:当前考点已不存在"
    )
    @ApiOperationSort(value = 6)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Object> update(@Valid @RequestBody OfficePlaceParam officePlaceParam) {
        log.info("# 修改考点参数 ： {}", officePlaceParam.toString());
        try {
            String officeId = getOfficeId();
            iOfficePlaceService.update(officePlaceParam, officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (GovInvalidRequestException i) {
            log.error("# 修改考点异常: {}", i.getMessage());
            return ApiResponse.builder()
                    .code(i.getCode())
                    .build();
        } catch (Exception e) {
            log.error("# 修改考点异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.PLACE_UPDATE_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "20.7 删除考点",
            notes = "30000:删除考点成功; 30156:删除考点异常; 30157:考点id不能为空"
    )
    @ApiOperationSort(value = 7)
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ApiResponse<Object> delete(@RequestBody String placeId) {
        log.info("# 删除考点参数 ： {}", placeId);
        try {
            if (StringUtils.isEmpty(placeId)) {
                //考点id不能为空
                return ApiResponse.builder()
                        .code(ResponseCode.PLACE_QUERY_ID_IS_NULL.getCode())
                        .build();
            }
            Map<String,String> map = JSON.parseObject(placeId,Map.class);
            iOfficePlaceService.delete(map.get("placeId"));
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (Exception e) {
            log.error("# 删除考点异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.PLACE_REMOVE_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "20.8 查询任务下所有考点",
            notes = "30000:查询任务下所有考点成功; 30139:查询任务下所有考点异常"
    )
    @ApiOperationSort(value = 8)
    @GetMapping("/getTaskPlace")
    public ApiResponse<Object> getTaskPlace(String taskId,String districtId) {
        try {

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(iTaskPlaceService.getTaskPlace(taskId, districtId))
                    .build();
        } catch (Exception e) {
            log.error("# 查询考点详细信息异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.QUERY_TASK_PLACE_INFO_EXCEPTION.getCode())
                    .build();
        }
    }


}
